package com.gongxt;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import com.gongxt.data.CallAndSmsHelper;

public class ContactPersonProvider extends AppWidgetProvider {
	private static String TAG = "ContactPersonProvider";

	@Override
	public void onEnabled(Context context) {
		Log.d(TAG, "onEnabled called");
		super.onEnabled(context);
	}
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		Log.i(TAG, "OnUpdate called");
		context.startService(new Intent(context, UpdateService.class));
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i(TAG, "onReceive called: " + intent.getAction().toString());
		super.onReceive(context, intent);
		
		String strAct = intent.getAction().toString();
		Intent i = new Intent(context, UpdateService.class);
		i.setAction(strAct);
		
		// dial action ---------------------------------
		
		if (context.getString(R.string.act_dial_favor_1).equals(intent.getAction().toString())
			|| context.getString(R.string.act_dial_favor_2).equals(intent.getAction().toString())
			|| context.getString(R.string.act_dial_favor_3).equals(intent.getAction().toString())){
			
			String strNumber = intent.getStringExtra(UpdateService.EXT_FAVOR_NUMBER);
			
			if (strNumber != null){
				Intent intentCall = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + strNumber));
				intentCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				context.startActivity(intentCall);
			}
			
		}else if (context.getString(R.string.act_show_dial_normal).equals(strAct) ||
				context.getString(R.string.act_show_dial_favor).equals(strAct)){
			context.startService(i);
		
		// sms action ------------------------------------
			
		}else if (context.getString(R.string.act_sms_favor_1).equals(intent.getAction().toString())
				|| context.getString(R.string.act_sms_favor_2).equals(intent.getAction().toString())
				|| context.getString(R.string.act_sms_favor_3).equals(intent.getAction().toString())){
				
				String strNumber = intent.getStringExtra(UpdateService.EXT_FAVOR_NUMBER);
								
				if (strNumber != null){
					Intent intentSms = new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + strNumber));
					intentSms.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					context.startActivity(intentSms);
				}
				
		}else if (context.getString(R.string.act_show_sms_normal).equals(strAct) ||
				context.getString(R.string.act_show_sms_favor).equals(strAct)){
			context.startService(i);
					
		// to system dial --------------------------------
			
		}else if (context.getString(R.string.act_to_system_dial).equals(strAct)){
			i = new Intent(Intent.ACTION_DIAL);
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(i);
			
		}else if (context.getString(R.string.act_to_system_miss_call).equals(strAct)){
			i = new Intent(Intent.ACTION_VIEW);
			i.setType("vnd.android.cursor.dir/calls");
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(i);
					
		// to system sms ---------------------------------
		
		}else if (context.getString(R.string.act_to_system_sms).equals(strAct)){

			i = new Intent(Intent.ACTION_VIEW);
			i.setType("vnd.android-dir/mms-sms");
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(i);
			
		}else if (context.getString(R.string.act_to_system_received_sms).equals(strAct)){
			i = new Intent(Intent.ACTION_VIEW);
			i.setType("vnd.android-dir/mms-sms");
			i.setData(Uri.parse("content://mms-sms/conversations/" + CallAndSmsHelper.getInstance(context).getLastSmsThreadID()));
			i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(i);
			
		}
	}

}
